#include <QApplication>
#include <QTreeView>
#include <QListView>
#include <QSplitter>
#include <QStringList>
#include <QAbstractListModel>

class StringListModel : public QAbstractListModel
{
    Q_OBJECT
public:
    StringListModel(const QStringList& list, QObject* parent = nullptr)
        : QAbstractListModel{parent}
        , m_list(list)
    {
    }

    int rowCount(const QModelIndex &parent = QModelIndex()) const
    {
        Q_UNUSED(parent); // 树形结构才会使用到parent
        return m_list.size();
    }

    QVariant data(const QModelIndex &index, int role) const
    {
        if (!index.isValid() || index.row() >= rowCount() || index.row() < 0)
            return {};
        switch(role) {
        case Qt::DisplayRole: { return m_list[index.row()]; }
        default: {return {};}
        }
        return {};
    }

private:
    QStringList m_list;
};


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSplitter* splitter = new QSplitter(Qt::Horizontal);

    QStringList str_list;
    str_list << "One" << "Two" << "Three" << "Four";

    StringListModel* model = new StringListModel(str_list);

    QTreeView* tree = new QTreeView(splitter);
    tree->setModel(model);

    QListView* list = new QListView(splitter);
    list->setModel(model);

    splitter->show();
    return a.exec();
}
